import { configureStore } from '@reduxjs/toolkit';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { combineReducers } from '@reduxjs/toolkit';
import logger from 'redux-logger';

// 导入slices
import userSlice from './slices/userSlice';
import userListSlice from './slices/userListSlice';

// 持久化配置
const persistConfig = {
    key: 'root',
    storage,
    whitelist: ['user', 'userList'], // 持久化user和userList状态
};

// 合并reducers
const rootReducer = combineReducers({
    user: userSlice,
    userList: userListSlice,
});

// 创建持久化reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// 配置store
export const store = configureStore({
    reducer: persistedReducer,
    middleware: (getDefaultMiddleware) =>
        getDefaultMiddleware({
            serializableCheck: {
                ignoredActions: ['persist/PERSIST', 'persist/REHYDRATE'],
            },
        }).concat(logger),
    devTools: import.meta.env.DEV,
});

// 创建persistor
export const persistor = persistStore(store);

// 导出类型
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;